package cn.com.blueInfo.bpm.person.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.person.entity.UserUnion;
import cn.com.blueInfo.bpm.person.mapper.UserUnionMapper;
import cn.com.blueInfo.bpm.person.repository.UserUnionRepository;
import cn.com.blueInfo.bpm.person.request.UserUnionDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 员工联合信息表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:28:09
 */
@Log4j2
@Service
public class UserUnionRepositoryImpl
        extends ServiceImpl<UserUnionMapper, UserUnion>
        implements UserUnionRepository, BaseRepository<UserUnion, UserUnionDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增员工联合信息
     * @param userUnion 员工联合信息参数
     */
    @Override
    public Integer createUserUnion(UserUnion userUnion) {
        return sqlExecuteResult(save(userUnion));
    }

    /**
     * 批量新增员工联合信息
     * @param userUnionList 员工联合信息参数列表
     */
    @Override
    public Integer batchCreateUserUnion(List<UserUnion> userUnionList) {
        return sqlExecuteResult(saveBatch(userUnionList), userUnionList.size());
    }

    /**
     * 删除员工联合信息
     * @param userUnionId 员工联合信息ID
     */
    @Override
    public Integer deleteUserUnion(Long userUnionId) {
        UserUnion userUnion = new UserUnion();
        userUnion.setId(userUnionId);
        userUnion.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(userUnion));
    }

    /**
     * 批量删除员工联合信息
     * @param userUnionIdList 员工联合信息ID列表
     */
    @Override
    public Integer batchDeleteUserUnion(List<Long> userUnionIdList) {
        List<UserUnion> userUnionList = new ArrayList<>();
        for (Long userUnionId : userUnionIdList) {
            UserUnion userUnion = new UserUnion();
            userUnion.setId(userUnionId);
            userUnion.setDeleted(BaseDeleted.DELETED);
            userUnionList.add(userUnion);
        }
        return sqlExecuteResult(removeBatchByIds(userUnionList), userUnionList.size());
    }

    /**
     * 更新部分员工联合信息
     * @param userUnion 员工联合信息参数
     */
    @Override
    public Integer updateUserUnionPartial(UserUnion userUnion) {
        validateUpdateId(userUnion.getId());
        LambdaUpdateWrapper<UserUnion> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserUnion::getId, userUnion.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部员工联合信息
     * @param userUnion 员工联合信息参数
     */
    @Override
    public Integer updateUserUnion(UserUnion userUnion) {
        validateUpdateId(userUnion.getId());
        return sqlExecuteResult(updateById(userUnion));
    }

    /**
     * 查询一条员工联合信息
     * @param userUnionId 员工联合信息ID
     */
    @Override
    public UserUnion getUserUnionById(Long userUnionId) {
        LambdaQueryWrapper<UserUnion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserUnion::getId, userUnionId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询员工联合信息
     * @param userUnionQueryDTO 员工联合信息参数
     */
    @Override
    public Page<UserUnion> getUserUnionPage(UserUnionDTO.QueryDTO userUnionQueryDTO) {
        Page<UserUnion> userUnionPage = new Page<>(
                userUnionQueryDTO.getPage(), userUnionQueryDTO.getSize());
        LambdaQueryWrapper<UserUnion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, userUnionQueryDTO);

        BaseDeleted deleted = userUnionQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(userUnionPage, queryWrapper);
        } else {
            return page(userUnionPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 员工联合信息 数量
     *
     * @param userUnionQueryDTO 员工联合信息参数
     */
    @Override
    public Long getUserUnionCount(UserUnionDTO.QueryDTO userUnionQueryDTO) {
        LambdaQueryWrapper<UserUnion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, userUnionQueryDTO);

        BaseDeleted deleted = userUnionQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 员工联合信息
     * @param userUnionQueryDTO 员工联合信息参数
     */
    @Override
    public List<UserUnion> getUserUnionList(UserUnionDTO.QueryDTO userUnionQueryDTO) {
        LambdaQueryWrapper<UserUnion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, userUnionQueryDTO);

        BaseDeleted deleted = userUnionQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<UserUnion> queryWrapper, UserUnionDTO.QueryDTO userUnionQueryDTO) {

        Long userIdentityId = userUnionQueryDTO.getUserIdentityId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(userIdentityId), UserUnion::getUserIdentityId, userIdentityId);

        Long userBaseId = userUnionQueryDTO.getUserBaseId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(userBaseId), UserUnion::getUserBaseId, userBaseId);

        Long userAccountId = userUnionQueryDTO.getUserAccountId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(userAccountId), UserUnion::getUserAccountId, userAccountId);

        Long orgLevelId = userUnionQueryDTO.getOrgLevelId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(orgLevelId), UserUnion::getOrgLevelId, orgLevelId);

        Long orgId = userUnionQueryDTO.getOrgId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(orgId), UserUnion::getOrgId, orgId);

        Long postDefId = userUnionQueryDTO.getPostDefId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(postDefId), UserUnion::getPostDefId, postDefId);

        Long postId = userUnionQueryDTO.getPostId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(postId), UserUnion::getPostId, postId);

        Long headshipId = userUnionQueryDTO.getHeadshipId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(headshipId), UserUnion::getHeadshipId, headshipId);

        addQueryWrapper(queryWrapper, userUnionQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<UserUnion, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                UserUnion::getOrderNumber,
                UserUnion::getTenantId,
                UserUnion::getApplicationId,
                UserUnion::getStatus,
                UserUnion::getDeleted,
                UserUnion::getDeleteUserId,
                UserUnion::getDeleteTime,
                UserUnion::getRemark,
                UserUnion::getCreateUserId,
                UserUnion::getCreateTime,
                UserUnion::getUpdateUserId,
                UserUnion::getUpdateTime,
                UserUnion::getExtend1,
                UserUnion::getExtend2,
                UserUnion::getExtend3);
    }

}
